<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用WeakSet操作ToDo列表</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }

        body{
            width: 100vw;
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        ul{
            list-style: none;
            display: flex;
            flex-direction: column;
        }

        li{
            width: 200px;
            height: 50px;
            border:2px solid orange;
            box-sizing: border-box;
            padding: 10px 20px;
            position: relative;
            margin-bottom: 20px;
        }

        li>a{
            width: 30px;
            height: 30px;
            background: green;
            color: white;
            position: absolute;
            top:10px;
            right: 10px;
            text-decoration: none;
            font-size: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        li.remove{
            border:2px solid #ddd;
        }

        li.remove>a{
            background: #ddd;
        }
    </style>
</head>
<body>
    <ul>
        <li>html<a href="javascript:void(0);">x</a></li>
        <li>css<a href="javascript:void(0);">x</a></li>
        <li>js<a href="javascript:void(0);">x</a></li>
    </ul>
    <script>
        class ToDo{
            constructor(){
                this.items = document.querySelectorAll('li');
                this.lists = new WeakSet();
                this.items.forEach(item => this.lists.add(item));
            }

            run(){
                this.addEvent();
            }

            addEvent(){
                // const _this = this.lists;
                // this.items.forEach(function (value) {
                //     let a = value.querySelector('a');
                //     a.addEventListener('click',event => {
                //         const parentParam = event.target.parentElement;
                //         if(_this.has(parentParam)){
                //             parentParam.classList.add("remove");
                //             _this.delete(parentParam);
                //         }else{
                //             parentParam.classList.remove("remove");
                //             _this.add(parentParam);
                //         }
                //     });
                // });

                this.items.forEach(value => {
                    let a = value.querySelector('a');
                    a.addEventListener('click',event => {
                        const parentParam = event.target.parentElement;
                        console.log(this);
                        if(this.lists.has(parentParam)){
                            parentParam.classList.add("remove");
                            this.lists.delete(parentParam);
                        }else{
                            parentParam.classList.remove("remove");
                            this.lists.add(parentParam);
                        }
                    });
                });
            }
        }
        new ToDo().run();
    </script>
</body>
</html>